package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.CuccServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.BusinessLevelAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Description:业务等级属性处理类
 * File Name:BusinessLevelAttrLinkHandler
 * Modification History:
 * Date             Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-12-13 11:09   邱萌         1.0       1.0 Version
 **/
@Service
@AttrHandler(BusinessLevelAttrHandler.class)
public class BusinessLevelAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if (Objects.isNull(attr)) {
            return;
        }

        //安徽个性需求
        //获取省份标识
        String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        //端口带宽
        KiteAttr dkdkKiteAttr = inst.getAttr("2_0000061");
        if (LocalKeyConsts.PROVINCE_AHHUI.equals(provinceCode) && KiteStringUtils.equalsAny(dkdkKiteAttr.getValue(),"1_0000289","1_0000295")) {
            IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
            String nodeCode = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "nodeCode");
            String serviceOfferId = data.getServiceOfferId();
            // app甩单补录、资源核查补录情况
            String renderOrderType = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "renderOrderType");
            if(KiteStringUtils.equalsAny(nodeCode,"OrderSigned","OrderResubmit","OrderAudit","OrderInput")||
                KiteStringUtils.equalsAny(serviceOfferId, LocalServiceOfferConsts.SERVICE_OFFER_FOLLOW,"1","2826",LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK,LocalServiceOfferConsts.SERVICE_OFFER_TRANSFER_TO_ORDER, CuccServiceOfferConsts.SERVICE_OFFER_BUSINESS_CHANGE)
                || KiteStringUtils.isEqual(renderOrderType, "orderRejection")){
                KiteAttr ywzlxKiteAttr = inst.getAttr(LocalKeyConsts.YWZLX_FIELD_NAME);
                List<KiteAttrValue> yezlxAttrList = ywzlxKiteAttr.getAttrValues();
                //业务子类型“上下行不对等互联网专线”去掉后就没有了，需要先判断，再补全
                boolean needAddFlag = true;
                for (KiteAttrValue value:yezlxAttrList) {
                    if(KiteStringUtils.equals("110009541",value.getAttrValue())){
                        //如果有的话，则不需要补
                        needAddFlag = false;
                    }
                }
                if(needAddFlag){
                    KiteAttrValue attrValue = new KiteAttrValue();
                    attrValue.setAttrValue("110009541");
                    attrValue.setAttrId("20210313608");
                    attrValue.setAttrValueName("上下行不对等互联网专线");
                    ywzlxKiteAttr.getAttrValues().add(attrValue);
                }
                KiteAttr sxslKiteAttr = inst.getAttr(LocalKeyConsts.SXSL_FIELD_NAME);
                KiteAttr xxslKiteAttr = inst.getAttr(LocalKeyConsts.XXSL_FIELD_NAME);
                if ("110009540".equals(attr.getValue())) {
                    if (Objects.nonNull(ywzlxKiteAttr)) {
                        operateInstAttrService.setAttrValue(inst, LocalKeyConsts.YWZLX_FIELD_NAME, "110009541", data.getSceneInstId(), null);
                        ywzlxKiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
                    }
                    if (Objects.nonNull(sxslKiteAttr)) {
                        sxslKiteAttr.setDefaultValue("");
                        sxslKiteAttr.setIsNull(KeyConsts.IFTRUE_F);
                        sxslKiteAttr.setAttrValues(this.GetSxslCode());
                        sxslKiteAttr.setGroupId("10000112701");
                        sxslKiteAttr.setGroupName("网络参数信息");
                        sxslKiteAttr.setIsEdit(KeyConsts.IFTRUE_T);
                        sxslKiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    }
                    if (Objects.nonNull(xxslKiteAttr)) {
                        if (Objects.nonNull(dkdkKiteAttr)){
                            operateInstAttrService.setAttrValue(inst, LocalKeyConsts.XXSL_FIELD_NAME, dkdkKiteAttr.getValue(), data.getSceneInstId(), null);
                        }
                        xxslKiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    }
                } else {
                    if (Objects.nonNull(ywzlxKiteAttr)) {
                        ywzlxKiteAttr.setIsEdit(KeyConsts.IFTRUE_T);
                        ywzlxKiteAttr.setValueDesc("");
                        operateInstAttrService.setAttrValue(inst, LocalKeyConsts.YWZLX_FIELD_NAME, "", data.getSceneInstId(), null);
                    }
                    if (Objects.nonNull(sxslKiteAttr)) {
                        sxslKiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                    }
                    if (Objects.nonNull(xxslKiteAttr)) {
                        operateInstAttrService.setAttrValue(inst, LocalKeyConsts.XXSL_FIELD_NAME, "", data.getSceneInstId(), null);
                        xxslKiteAttr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
                    }
                    //业务子类型去掉“上下行不对等互联网专线”
                    ywzlxKiteAttr.getAttrValues().removeIf(ywzlxAttr -> KiteStringUtils.equals(ywzlxAttr.getAttrValue(), "110009541"));
                }
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr kiteAttr, KiteInst kiteInst, AttrData attrData) {

    }

    private static  List<KiteAttrValue> GetSxslCode() {
        List<KiteAttrValue> attrValues = new ArrayList<>();
        KiteAttrValue attrValue = new KiteAttrValue();
        attrValue.setAttrValue("1_0000277");
        attrValue.setAttrId("202106021117");
        attrValue.setSortBy("1");
        attrValue.setAttrValueName("30MBPS");
        attrValues.add(attrValue);
        KiteAttrValue attrValue1 = new KiteAttrValue();
        attrValue1.setAttrValue("1_0000282");
        attrValue1.setAttrId("202106021117");
        attrValue1.setSortBy("2");
        attrValue1.setAttrValueName("50MBPS");
        attrValues.add(attrValue1);
        KiteAttrValue attrValue2 = new KiteAttrValue();
        attrValue2.setAttrValue("1_0000289");
        attrValue2.setAttrId("202106021117");
        attrValue2.setSortBy("3");
        attrValue2.setAttrValueName("100MBPS");
        attrValues.add(attrValue2);
        return attrValues;
    }
}
